<?php

class MStorageWarehouse extends MY_Model
{

    const STATE_NOT_ACCEPT = 0;
    const STATE_ACCEPTED = 1;

    //排序方式
    const ORDER_ID_DESC = 1;
    const ORDER_ID_ASC = 2;
    const ORDER_TASKOPERATE_NOTDEFINE_DESC = 3;
    const ORDER_TASKOPERATE_NOTDEFINE_ASC = 4;

    //是否加急
    const IS_URGENT_FALSE = 0;
    const IS_URGENT_TRUE = 1;
    protected $table = 'storage_warehouse';
    private $filter = array();
    public $has_accept;

    function __construct()
    {
        parent::__construct();
    }

    public static function getState($state = NULL)
    {
        $define = array(self::STATE_ACCEPTED => '<span class="label label-success">可用</span>', self::STATE_NOT_ACCEPT => '<span class="label label-danger">禁用</span>',);
        if ($state === NULL) {
            return $define;
        } else {
            return key_exists($state, $define) ? $define[$state] : '';
        }
    }

    public static function getOrderType($orderType = NULL)
    {
        $define = array(
            self::ORDER_ID_DESC => 'ID倒序',
            self::ORDER_ID_ASC => 'ID升序',
            self::ORDER_TASKOPERATE_NOTDEFINE_DESC => '未审核数倒序',
            self::ORDER_TASKOPERATE_NOTDEFINE_ASC => '未审核数升序'

        );
        if ($orderType === NULL) {
            return $define;
        } else {
            return key_exists($orderType, $define) ? $define[$orderType] : '';
        }
    }


    public function getWareList($limit, $product_id)
    {
        $this->db->select('*');
        $this->db->distinct();
        $this->db->from($this->table);
        $this->db->where('product_id', $product_id);
        $this->db->limit($limit['limit'], $limit['offset']);
        $this->formfilter->doFilter();

        $query = $this->db->get();
        return $query->result();
    }


    public function getWareCount($product_id)
    {
        $this->db->select('count(distinct id) as num');
        $this->db->from($this->table);
        $this->db->where('product_id', $product_id);
        $this->formfilter->doFilter();
        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getQuantity($product_id)
    {
        $this->db->select('sum(left_number) as num');
        $this->db->from($this->table);
        $this->db->where('product_id', $product_id);

        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getById($id)
    {
        $query = $this->db->where('id', $id)->get($this->table);
        return $query->row();
    }

    public function getHousing($product_id)
    {
        $this->db->select("*");
        $this->db->from($this->table);
        $this->db->group_by('warehousing');
        $this->db->where('product_id', $product_id);
        return $this->db->get()->result();
    }

    public function getStorage($product_id)
    {
        $this->db->select("*,count(left_number) as left_nums");
        $this->db->from($this->table);
        $this->db->group_by('product_id');
        $this->db->where('product_id', $product_id);
        return $this->db->get()->row();
    }

    public function ascStorageLeftNum($product_id, $warehousing)
    {
        $this->db->select("*,count(left_number) as left_nums");
        $this->db->from($this->table);
        $this->db->group_by('product_id');
        $this->db->where('product_id', $product_id);
        return $this->db->get()->row();
    }

    public function getCountNum($time_begin, $time_end, $type, $location,$shop_id)
    {
        $this->db->select('r.*,w.location');
        $this->db->from('storage_record r');
        $this->db->join($this->table.' w','r.warehousing = w.warehousing and r.product_id = w.product_id','INNER');
        $this->db->where('r.status', 1);
        if ($shop_id){
            $this->db->where('r.shop_id', $shop_id);
        }

        if($location){
            $this->db->where('w.location', $location);
        }
        if ($type == 'init') {
            $format = 'Y-m-d';
            $x = $this->get_weeks();
            $this->db->where('r.create_time >= ', strtotime($x[0] . '00:00:00'));
            $this->db->where('r.create_time <= ', strtotime($x[6] . '23:59:59'));
        } else {
            $begin = strtotime($time_begin);
            $end = strtotime($time_end);
            $days = ($end - $begin) / 86400;
            if ($days > 0 && $days <= 30) {
                $line_type = 3;
                $format = 'Y-m-d';
            } else if ($days > 30 && $days <= 365) {
                $line_type = 2;
                $format = 'Y-m';
            } else {
                $line_type = 1;
                $format = 'Y';
            }
            $x = $this->getDateFromRange($time_begin, $time_end, $line_type);
            if ($line_type == 3) {
                $time_format_begin = $time_begin . ' 00:00:00';
                $time_format_end = $time_end . ' 23:59:59';
            } else {
                $time_format_begin = $time_begin;
                $time_format_end = $time_end;
            }

            if ($time_begin) {
                $this->db->where('r.create_time >= ', strtotime($time_format_begin));
            }
            if ($time_end) {
                $this->db->where('r.create_time <= ', strtotime($time_format_end));
            }
        }
        $list = $this->db->get()->result();
        $asc_arr = array();
        $desc_arr = array();
        if (count($list)) {
            foreach ($list as $v) {
                $v->create_time = date($format, $v->create_time);
            }
            foreach ($x as $x_v) {
                $asc = 0;
                $desc = 0;
                foreach ($list as $key => $value) {
                    if ($x_v == $value->create_time) {
                        if ($value->type) {
                            $asc += $value->amount;
                        } else {
                            $desc -= $value->amount;
                        }
                    }
                    $asc_arr[$x_v] = $asc;
                    $desc_arr[$x_v] = $desc;
                }
            }
        } else {
            foreach ($x as $x_v) {
                $asc_arr[$x_v] = 0;
                $desc_arr[$x_v] = 0;
            }
        }

        return [
            'success' => 1,
            'msg' => '获取成功',
            'x' => $x,
            'asc' => array_values($asc_arr),
            'desc' => array_values($desc_arr),
            'origin' => $list
        ];
    }

    public function getLineNum($time_begin, $time_end, $type, $location,$shop_id)
    {
        $this->db->select('r.*,w.location');
        $this->db->from('storage_record r');
        $this->db->join($this->table.' w','r.warehousing = w.warehousing and r.product_id = w.product_id','INNER');
        $this->db->where('r.status', 1);
        if ($shop_id){
            $this->db->where('r.shop_id', $shop_id);
        }
        if($location){
            $this->db->where('w.location', $location);
        }
        if ($type == 'init') {
            $format = 'Y-m-d';
            $x = $this->get_weeks();
            $this->db->where('r.create_time <= ', strtotime($x[6] . '23:59:59'));
        } else {
            $begin = strtotime($time_begin);
            $end = strtotime($time_end);
            $days = ($end - $begin) / 86400;
            if ($days > 0 && $days <= 30) {
                $line_type = 3;
                $format = 'Y-m-d';
            } else if ($days > 30 && $days <= 365) {
                $line_type = 2;
                $format = 'Y-m';
            } else {
                $line_type = 1;
                $format = 'Y';
            }
            $x = $this->getDateFromRange($time_begin, $time_end, $line_type);
            if ($line_type == 3) {
                $time_format_end = $time_end . ' 23:59:59';
            } else {
                $time_format_end = $time_end;
            }

            if ($time_end) {
                $this->db->where('r.create_time <= ', strtotime($time_format_end));
            }
        }
        $list = $this->db->get()->result();
        $amount = array();
        if (count($list)) {
            foreach ($list as $v) {
                $v->create_time = date($format, $v->create_time);
            }
            foreach ($x as $x_v) {
                $sum = 0;
                foreach ($list as $key => $value) {
                    if (strtotime($x_v) >= strtotime($value->create_time)) {
                        if ($value->type) {
                            $sum += $value->amount;
                        } else {
                            $sum -= $value->amount;
                        }

                    } else {
                        $sum += 0;
                    }
                    $amount[$x_v] = $sum;
                }
            }
        } else {
            foreach ($x as $x_v) {
                $amount[$x_v] = 0;
            }
        }

        return [
            'success' => 1,
            'msg' => '获取成功',
            'x' => $x,
            'data' => array_values($amount)
        ];
    }
}
